package zjs.dc.controller;

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

/**
 * @Class:
 * @Author: guoml
 * @Description: 针对企查查t_copyrights_copyright表和中数t_productcopyrightinfo表进行数据映射开发
 * @Date: 2021/12/7 12:51
 */

public class TCopyrightsCopyright {
    public static void main(String[] args) {
        SparkSession spark = SparkSession.builder().appName("t_copyrights_copyright").enableHiveSupport().getOrCreate();
        spark.sparkContext().setLogLevel("ERROR");
        Dataset<Row> zs_data = spark.sql("select * from a_dc_ep_ods.zs_t_productcopyrightinfo");
        long zs_count = zs_data.count();
        if(zs_count>0){

            //获取中数增量数据表中的更新数据（inner join）
            Dataset<Row> update_data = spark.sql("SELECT a.id,\n" +
                    "       a.key_no,\n" +
                    "       a.company_id,\n" +
                    "       d.company_name,\n" +
                    "       a.copyright_id,\n" +
                    "       b.FZD_ZPLB,\n" +
                    "       b.FZD_ZPMC,\n" +
                    "       b.FZD_ZZQRNAME,\n" +
                    "       b.FZD_DJH,\n" +
                    "       b.FZD_DJDATE,\n" +
                    "       b.FZD_CZWCDATE,\n" +
                    "       b.FZD_SCFBDATE,\n" +
                    "       b.JOBID,\n" +
                    "       \"0\" as isadd\n" +
                    "FROM\n" +
                    "  (SELECT *\n" +
                    "   FROM a_dc_ep_ods.zs_t_productcopyrightinfo\n" +
                    "   WHERE CREDITCODE IS NOT NULL\n" +
                    "     AND CREDITCODE !='') b\n" +
                    "LEFT JOIN\n" +
                    "  a_dc_ep_ods.t_eci_company d on upper(trim(d.credit_code)) = upper(trim(b.CREDITCODE))\n" +
                    "     AND d.credit_code IS NOT NULL and d.credit_code!=''\n" +
                    "INNER JOIN\n" +
                    "  (SELECT m.*,n.credit_code\n" +
                    "   FROM a_dc_ep_dwi.t_copyrights_copyright m\n" +
                    "INNER JOIN\n" +
                    "   a_dc_ep_ods.t_eci_company n\n" +
                    "ON upper(trim(m.company_id)) = upper(trim(n.company_id))\n" +
                    "   WHERE n.credit_code IS NOT NULL\n" +
                    "     AND n.credit_code!='') a ON upper(trim(a.credit_code)) = upper(trim(b.CREDITCODE)) and a.register_no = b.FZD_DJH");

            update_data.createOrReplaceTempView("tmp_t_copyrights_copyright");
            spark.sql("insert overwrite table a_dc_ep_incr.t_copyrights_copyright select * from tmp_t_copyrights_copyright");
            //获取中数增量数据表中的新增数据（left join）
            Dataset<Row> insert_data = spark.sql("SELECT b.RECORD_ID,\n" +
                    "       d.key_no,\n" +
                    "       d.company_id,\n" +
                    "       d.company_name,\n" +
                    "       '' as copyright_id,\n" +
                    "       b.FZD_ZPLB,\n" +
                    "       b.FZD_ZPMC,\n" +
                    "       b.FZD_ZZQRNAME,\n" +
                    "       b.FZD_DJH,\n" +
                    "       b.FZD_DJDATE,\n" +
                    "       b.FZD_CZWCDATE,\n" +
                    "       b.FZD_SCFBDATE,\n" +
                    "       b.JOBID,\n" +
                    "       \"1\" as isadd\n" +
                    "FROM\n" +
                    "  (SELECT *\n" +
                    "   FROM a_dc_ep_ods.zs_t_productcopyrightinfo\n" +
                    "   WHERE CREDITCODE IS NOT NULL\n" +
                    "     AND CREDITCODE !='') b\n" +
                    "LEFT JOIN\n" +
                    "  a_dc_ep_ods.t_eci_company d on upper(trim(d.credit_code)) = upper(trim(b.CREDITCODE))\n" +
                    "     AND d.credit_code IS NOT NULL and d.credit_code!=''\n" +
                    "LEFT JOIN\n" +
                    "  (SELECT m.*,n.credit_code\n" +
                    "   FROM a_dc_ep_dwi.t_copyrights_copyright m\n" +
                    "INNER JOIN\n" +
                    "   a_dc_ep_ods.t_eci_company n\n" +
                    "ON upper(trim(m.company_id)) = upper(trim(n.company_id))\n" +
                    "   WHERE n.credit_code IS NOT NULL\n" +
                    "     AND n.credit_code!='') a ON upper(trim(a.credit_code)) = upper(trim(b.creditcode)) and a.register_no = b.FZD_DJH\n" +
                    "WHERE a.credit_code IS NULL");
            insert_data.createOrReplaceTempView("tmp_t_copyrights_copyright1");
            spark.sql("insert into table a_dc_ep_incr.t_copyrights_copyright select * from tmp_t_copyrights_copyright1");
            //获取中数删除数据表中的数据
            Dataset<Row> delete_data = spark.sql("select a.id,a.key_no,a.company_id,a.company_name,a.copyright_id,a.category,a.name,a.owner,a.register_no,a.register_date,a.finish_date,a.publish_date,a.dates,'-1' as isadd from (select * from a_dc_ep_dwi.t_copyrights_copyright)a inner join (select m.* from a_dc_ep_ods.zs_t_productcopyrightinfo_del m left join a_dc_ep_ods.zs_t_productcopyrightinfo n on m.FZD_DJH = n.FZD_DJH where n.FZD_DJH is null)b on upper(trim(a.register_no)) = upper(trim(b.FZD_DJH))");
            delete_data.createOrReplaceTempView("tmp_t_copyrights_copyright2");
            spark.sql("insert into table a_dc_ep_incr.t_copyrights_copyright select * from tmp_t_copyrights_copyright2");
        }else {
            spark.sql("TRUNCATE TABLE a_dc_ep_incr.t_copyrights_copyright");
            System.out.println("中数本期数据涉及该表的数据为空。。。。。。");
        }
        spark.stop();
    }
}
